$(function() {
    // 读取body data-type 判断是哪个页面然后执行相应页面方法，方法在下面。
    var dataType = $('body').attr('data-type');
    //     // 判断用户是否已有自己选择的模板风格
    //    if(storageLoad('SelcetColor')){
    //      $('body').attr('class',storageLoad('SelcetColor').Color)
    //    }else{
    //        storageSave(saveSelectColor);
    //        $('body').attr('class','theme-black')
    //    }

    autoLeftNav();
    $(window).resize(function() {
        autoLeftNav();
    });

    //    if(storageLoad('SelcetColor')){

    //     }else{
    //       storageSave(saveSelectColor);
    //     }
})

// 风格切换

$('.tpl-skiner-toggle').on('click', function() {
    $('.tpl-skiner').toggleClass('active');
})

$('.tpl-skiner-content-bar').find('span').on('click', function() {
    $('body').attr('class', $(this).attr('data-color'))
    saveSelectColor.Color = $(this).attr('data-color');
    // 保存选择项
    storageSave(saveSelectColor);

})




// 侧边菜单开关


function autoLeftNav() {

    $('.tpl-header-switch-button').on('click', function() {
        if ($('.left-sidebar').is('.active')) {
            if ($(window).width() > 1024) {
                $('.tpl-content-wrapper').removeClass('active');
            }
            $('.left-sidebar').removeClass('active');
        } else {

            $('.left-sidebar').addClass('active');
            if ($(window).width() > 1024) {
                $('.tpl-content-wrapper').addClass('active');
            }
        }
    })

    if ($(window).width() < 1024) {
        $('.left-sidebar').addClass('active');
    } else {
        $('.left-sidebar').removeClass('active');
    }
}


// 侧边菜单
$('.sidebar-nav-sub-title').on('click', function() {
    $(this).siblings('.sidebar-nav-sub').slideToggle(80)
        .end()
        .find('.sidebar-nav-sub-ico').toggleClass('sidebar-nav-sub-ico-rotate');
})

//屏幕高度自适应
$('.widget').css("height",window.outerHeight-59);

var localApi2 = "http://47.97.8.254:8080/qk_code_api";
//var localApi = "http://127.0.0.1:8080/QingkeCodeApi";
// var localApi = "http://192.168.1.130:8080/QingKeExam";
//var localApi = "http://127.0.0.1:8080/QingKeExam"
//  var localApi = "http://47.97.8.254:8080/QingKeExam";
//  var localApi = "http://192.168.1.130:8080/QingKeExam";
 var localApi = "http://47.97.8.254:8080/QingKeExam";
//判断用户是否已经登录
var currentUser = $.cookie('currentUser');
var userInfo = $.cookie('userInfo');
//origin  返回站点主地址
var origin = window.location.origin;
var webName = window.location.pathname.split("/")[1];
if (window.location.pathname !="/login.html") {
    if ((typeof(userInfo)== "undefined") || (userInfo.endsWith("null")) || (userInfo.length == 0)) {
        window.location.href=origin+"/login.html";
    } else{
        var username = $.cookie('username');
        $('.user-panel-logged-in-text').text(username);
        $(".userName").text(username);
    }
}

//用户退出
$(".sign-out").click(function(){
//	if (($("#my-confirm") != null) || ($("#my-confirm") != 'undefined')) {
//		$('#my-confirm .am-modal-hd').text("提示");
//		$('#my-confirm .am-modal-bd').text("你，确定要退出吗?");
//	} else{
    var notice = '<div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm-signout"><div class="am-modal-dialog">'+
        '<div class="am-modal-hd">提示</div><div class="am-modal-bd">你，确定要退出吗？</div>'+
        '<div class="am-modal-footer"><span class="am-modal-btn" data-am-modal-cancel>取消</span>'+
        '<span class="am-modal-btn" data-am-modal-confirm>确定</span></div></div></div>'
    $("body").append(notice);
//	}
    $('#my-confirm-signout').modal({
        relatedTarget: this,
        onConfirm: function(options) {
            $.ajax({
                type: "get",
                url: localApi + "/Admin/loginout",
                data:{
                    apikey:'1'
                },
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function(result) {
                    $.cookie('userInfo',null,{domain: '47.97.8.254',
                        path: '/'});
                        $.cookie('adminid',null,{domain: '47.97.8.254',
                        path: '/'});
                    window.location.href=origin+"/login.html";
                }
            });
        }
    });

});


//时间戳转化
function formatDate(nows) {
    var now = new Date(nows);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
};

$.ajaxSetup({
    xhrFields: {
        withCredentials: true
    },
    crossDomain: true,
    contentType: "application/x-www-form-urlencoded;charset=utf-8",
});


